package org.example.cake.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.example.cake.common.Rep;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.example.cake.entity.User;
import org.example.cake.mapper.UserMapper;
import org.example.cake.service.UserService;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer userid) {
        return userMapper.deleteByPrimaryKey(userid);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    /**
     * 查询或注册用户 如果有通过 如果没有就注册
     * @param user
     * @date 2025-07-25 谭丽娜
     * @return
     */
    @Override
    public void findOrCreateUser(User user) {
        //通过openid查询用户是否存在
        User selectuseropenid = userMapper.selectuseropenid(user.getOpenid());
        if(selectuseropenid==null) {
            //没有就注册一个
            userMapper.insert(user);
        }else {
            //修改用户信息
            user.setUserid(selectuseropenid.getUserid());
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public User getUserInfo(String code) {
        return userMapper.selectuseropenid(code);
    }

    /**
     *  分页查询所有用户
     * @param username 用户名
     * @param userphone 用户手机号
     * @param status 用户状态
     * @return 用户列表
     */
    @Override
    public List<User> SelectUserHou(String username, String userphone,Integer status) {
        return userMapper.SelectUserHou( username, userphone, status);
    }
    /**
     * 根据id删除用户
     * @param ids 用户id
     * @return 删除结果
     * */
    @Override
    public int toggleStatus(List<Long> ids) {
        return userMapper.toggleStatus(ids);
    }


    @Override
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public User getUserById(Integer userId) {
        if (userId == null || userId <= 0) {
            throw new IllegalArgumentException("用户ID不能为空且必须大于0");
        }
        return userMapper.selectUserById(userId);
    }

    /**
     * 修改用户信息
     * @param user 用户对象
     * @return 受影响的行数
     */
    public int updateUser(User user){
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 根据用户id查询用户信息
     * @param userid 用户id
     * @return 用户对象
     */
    public User selectUserByUserId(@Param("userid") Integer userid){
        return userMapper.selectUserByUserId(userid);
    }
}
